package com.singer.druid;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.singer.domain.Judge;
import com.singer.domain.SingerEntity;
import com.singer.piping.CompetitionJdbc;
import com.singer.piping.Score;
import com.singer.piping.method.SingerScore;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

public class Competition implements CompetitionJdbc {
    @Override
    public void competition() throws Exception {
        Properties prop = new Properties();

        prop.load(new FileInputStream("src/druid.properties"));

        DataSource data = DruidDataSourceFactory.createDataSource(prop);

        Connection connection = data.getConnection();
        String sql ="select*from t_singer";
        PreparedStatement sr= connection.prepareStatement(sql);
        ResultSet rs=sr.executeQuery();
        SingerEntity singer = null;

        List<SingerEntity> sew=new ArrayList<>();
        while (rs.next()){
            int id=   rs.getInt("id");
            String names= rs.getString("name");
            String song=rs.getString("song");
            singer=new SingerEntity();
            singer.setId(id);
            singer.setName(names);
            singer.setSong(song);
            sew.add(singer);
        }
        rs.close();
        sr.close();
        connection.close();

        Properties props = new Properties();

        props.load(new FileInputStream("src/druid.properties"));

        DataSource datas = DruidDataSourceFactory.createDataSource(prop);

        Connection connections = datas.getConnection();
        String sql1 ="select*from t_judge";

        PreparedStatement sr1= connections.prepareStatement(sql1);
        ResultSet we=sr1.executeQuery();
        Judge judge;
        List<Judge> ser=new ArrayList<>();
        while (we.next()){
            int id= we.getInt("id");
            String names= we.getString("name");
            judge= new Judge();
            judge.setId(id);
            judge.setName(names);
            ser.add(judge);
        }
        we.close();
        sr1.close();
        connections.close();
        List<Double> list1=new ArrayList<>();
        List<Double> list=new ArrayList<>();
        Scanner pe=new Scanner(System.in);
        for (int i=0; i<sew.size() ; i++) {
            System.out.println("由第"+sew.get(i).getId()+"位歌手"+sew.get(i).getName()+"演唱:"+sew.get(i).getSong());
            for (int j = 0; j <ser.size() ; j++) {
                System.out.println("由第"+ser.get(j).getId()+"位裁判"+ser.get(j).getName()+"打分:");
                double scores= pe.nextDouble();
                list.add(scores);
            }
            double a=list.get(0);
            for (int j = 1; j < list.size(); j++) {
                if (list.get(j)>a){
                    a=list.get(j);
                }
            }

            double b=list.get(0);
            for (int j = 1; j < list.size(); j++) {
                if (list.get(j)<b){
                    b=list.get(j);
                }
            }
            double c=0;
            for (int w = 0; w < list.size(); w++) {
                if (list.get(w)!=a&&list.get(w)!=b){
                    c+=list.get(w);
                }
            }
            double score= c/(list.size()-2);
            list.clear();
            list1.add(score);
            Score se=new SingerScore();
            se.score(i+1,score);
        }
    }
}




